package com.ruoyi.web.controller.news;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.news.NewsCenterIntroduction;
import com.ruoyi.system.domain.news.dto.NewsCenterIntroductionDto;
import com.ruoyi.system.domain.news.query.NewsCenterIntroductionQuery;
import com.ruoyi.system.domain.news.vo.NewsCenterIntroductionVo;
import com.ruoyi.system.service.news.NewsCenterIntroductionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/news/newsCenterIntroduction")
@Api(value = "newsCenterIntroductionController", tags  = "关于等保")
public class NewsCenterIntroductionController extends BaseController {

	@Resource
	private NewsCenterIntroductionService newsCenterIntroductionService;

    @ApiOperation(value = "新增/修改关于等保信息", notes = "新增/修改关于等保信息")
	@PostMapping("/submit")
	public R submit(@RequestBody NewsCenterIntroductionDto newsCenterIntroductionDto) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		NewsCenterIntroduction newsCenterIntroduction = new NewsCenterIntroduction();
		BeanUtils.copyProperties(newsCenterIntroductionDto,newsCenterIntroduction);
		if (newsCenterIntroduction.getId()==null) {
			newsCenterIntroduction.setCreateBy(loginUser.getUsername());
			newsCenterIntroduction.setDeptId(loginUser.getDeptId());
			newsCenterIntroduction.setCreateTime(new Date());
			newsCenterIntroduction.setUpdateBy(loginUser.getUsername());
			newsCenterIntroduction.setUpdateTime(new Date());
		} else {
			newsCenterIntroduction.setUpdateBy(loginUser.getUsername());
			newsCenterIntroduction.setUpdateTime(new Date());
		}
		boolean result = newsCenterIntroductionService.saveOrUpdate(newsCenterIntroduction);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "删除关于等保信息", notes = "删除关于等保信息")
	@DeleteMapping("/delete")
	public R delete(@RequestParam(value = "id") Long id) {
		boolean result = newsCenterIntroductionService.removeById(id);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "获取单个关于等保信息", notes = "获取单个关于等保信息")
	@GetMapping("/getById")
	public R<NewsCenterIntroductionVo> getById(@RequestParam(value = "id") Long id) {
		NewsCenterIntroductionQuery newsCenterIntroductionQuery = new NewsCenterIntroductionQuery();
		newsCenterIntroductionQuery.setId(id);
		List<NewsCenterIntroductionVo> newsCenterIntroductionList = newsCenterIntroductionService.getNewsCenterIntroductionList(newsCenterIntroductionQuery);
		if (newsCenterIntroductionList!=null&&newsCenterIntroductionList.size()>0) {
			return R.ok(newsCenterIntroductionList.get(0));
		} else {
			return R.fail("数据不存在");
		}
	}

	@ApiOperation(value = "获取关于等保信息列表")
	@PostMapping(value = "/list")
	public R<PageInfo<NewsCenterIntroductionVo>> list(@RequestBody NewsCenterIntroductionQuery newsCenterIntroductionQuery) {
		List<NewsCenterIntroductionVo> newsCenterIntroductionList = newsCenterIntroductionService.getNewsCenterIntroductionList(newsCenterIntroductionQuery);
		return R.ok(new PageInfo<>(newsCenterIntroductionList));
	}
}
